/**************************************************************************
 *                                                                        *
 *         Copyright (c) 2007 by Sunplus mMedia Inc.                      *
 *                                                                        *
 *  This software is copyrighted by and is the property of Sunplus        *
 *  mMedia Inc. All rights are reserved by Sunplus mMedia Inc.  This      *
 *  software may only be used in accordance with the corresponding        *
 *  license agreement. Any unauthorized use, duplication, distribution,   *
 *  or disclosure of this software is expressly forbidden.                *
 *                                                                        *
 *  This Copyright notice MUST not be removed or modified without prior   *
 *  written consent of Sunplus mMedia Inc.                                *
 *                                                                        *
 *  Sunplus mMedia Inc. reserves the right to modify this software        *
 *  without notice.                                                       *
 *                                                                        *
 *  Sunplus mMedia Inc.                                                   *
 *  19-1, Innovation First Road, Science-Based Industrial Park,           *
 *  Hsin-Chu, Taiwan, R.O.C.                                              *
 *                                                                        *
 **************************************************************************/


#ifndef __KEY_SCAN_H__
#define __KEY_SCAN_H__
//=============================================================================
//Header file
//=============================================================================
#include "button_def.h"
//=============================================================================
//Symbol
//=============================================================================]

//------------------------------------------------------------------------------
//Constant
//------------------------------------------------------------------------------

#define SP1K_GPIO_RPT           3  /* was 12, 0 ~ 16; 0:No repeate key; 1:More frequently*/
#define SP1K_GPIO_RPT_START		10 //define the delay time to do the first repeat key

#define KEY_SCAN_ADC_VAL_MIN			0x0000U	

#define KEY_SCAN_ADC_VAL_MAX			0x03ffU

#define KEY_SCAN_ADC_A_VAL_IDLE		(KEY_SCAN_ADC_VAL_MAX - 0x0fU)
#if (_HW_SOLUTION_ == _HW_EVB_ || _HW_SOLUTION_ == _HW_TK_MODEL_A_)
#define KEY_SCAN_ADC_B_VAL_IDLE		(KEY_SCAN_ADC_VAL_MAX - 0x0fU)
#elif (_HW_SOLUTION_ == _HW_TK_MODEL_B_)
#define KEY_SCAN_ADC_B_VAL_IDLE		(KEY_SCAN_ADC_VAL_MIN + 0x0fU)
#endif

#define KEY_SCAN_ADC_VAL_RPT			(SP1K_GPIO_RPT)	//Key scan: 10ms
#define KEY_SCAN_ADC_VAL_RPT_START		(SP1K_GPIO_RPT_START)

#define KEY_SCAN_ADC_VAL_DEC			2


/* ADC button val*/
#define SP1K_ADC_BTN_A_S1			0		/* RIGHT */
#define SP1K_ADC_BTN_A_S2			71		/* LEFT */
#define SP1K_ADC_BTN_A_S3			174		/* DOWN */
#define SP1K_ADC_BTN_A_S4			332		/* UP */
#define SP1K_ADC_BTN_A_S5			494		/* TELE */
#define SP1K_ADC_BTN_A_S6			684		/* WIDE */

#if (_HW_SOLUTION_ == _HW_EVB_)
#define SP1K_ADC_BTN_B_S1			174		/* F2 */
#define SP1K_ADC_BTN_B_S2         	332		/* PB */
#define SP1K_ADC_BTN_B_S3			494		/* F1 */
#define SP1K_ADC_BTN_B_S4			684		/* OK/MENU */
#elif (_HW_SOLUTION_ == _HW_TK_MODEL_A_)
#define SP1K_ADC_BTN_B_S1         	326		/* DISP */
#define SP1K_ADC_BTN_B_S2			490		/* MODE */
#define SP1K_ADC_BTN_B_S3			685		/* OK/MENU */

#elif (_HW_SOLUTION_ == _HW_TK_MODEL_B_)
#define SP1K_ADC_BTN_B_S1			170//224		/* POWER */
#define SP1K_ADC_BTN_B_S2         	326//373		/* DISP */
#define SP1K_ADC_BTN_B_S3			490//506		/* MODE */
#define SP1K_ADC_BTN_B_S4			685//684		/* OK/MENU */
#endif //hw solution


/* ADC button dec*/

#define SP1K_ADC_BTN_DEV_A_S1		10		
#define SP1K_ADC_BTN_DEV_A_S2		13		
#define SP1K_ADC_BTN_DEV_A_S3		31		
#define SP1K_ADC_BTN_DEV_A_S4		54		
#define SP1K_ADC_BTN_DEV_A_S5		77		
#define SP1K_ADC_BTN_DEV_A_S6		94

#if (_HW_SOLUTION_ == _HW_EVB_)
#define SP1K_ADC_BTN_DEV_B_S1		31		
#define SP1K_ADC_BTN_DEV_B_S2		54		
#define SP1K_ADC_BTN_DEV_B_S3		77		
#define SP1K_ADC_BTN_DEV_B_S4		94	
#elif(_HW_SOLUTION_ == _HW_TK_MODEL_A_)		
#define SP1K_ADC_BTN_DEV_B_S1		52		
#define SP1K_ADC_BTN_DEV_B_S2		78		
#define SP1K_ADC_BTN_DEV_B_S3		95
#elif (_HW_SOLUTION_ == _HW_TK_MODEL_B_)
#define SP1K_ADC_BTN_DEV_B_S1		29		
#define SP1K_ADC_BTN_DEV_B_S2		52		
#define SP1K_ADC_BTN_DEV_B_S3		78		
#define SP1K_ADC_BTN_DEV_B_S4		95	
#endif

/* ADC button num*/

#define SP1K_ADC_BTN_A_NUM		6
		
#if (_HW_SOLUTION_ == _HW_EVB_)
#define SP1K_ADC_BTN_B_NUM		4
#elif (_HW_SOLUTION_ == _HW_TK_MODEL_A_)
#define SP1K_ADC_BTN_B_NUM		3
#elif (_HW_SOLUTION_ == _HW_TK_MODEL_B_)
#define SP1K_ADC_BTN_B_NUM		4
#endif

#define SP1K_ADC_BTN_NUM			(SP1K_ADC_BTN_A_NUM + SP1K_ADC_BTN_B_NUM)


/*GPIO button status*/
#define SP1K_GPIO_BTN_A_STATUS	KEY_STATUS_RELEASED		/*Power*/
#define SP1K_GPIO_BTN_B_STATUS	KEY_STATUS_RELEASED		/*S2*/

/*GPIO button num*/
#define SP1K_GPIO_BTN_NUM			2


#define SP1K_BTN_NUM				(SP1K_ADC_BTN_NUM + SP1K_GPIO_BTN_NUM)

#define POWER_BAT_DEC		5
#define BATT_LOW_POWER_OFF_RETRY		((UINT16)100)		//100*30ms

//------------------------------------------------------------------------------
//Data type
//------------------------------------------------------------------------------
typedef enum keyStatus_e {
	KEY_STATUS_PRESSED = (unsigned char)0x01,
	KEY_STATUS_RELEASED = (unsigned char)0x02,
	KEY_STATUS_REPEAT = (unsigned char)0x04,	
} keyStatus_t;

typedef enum keyType_e {
	KEY_TYPE_ADC_A = (unsigned char)0x01,
	KEY_TYPE_ADC_B = (unsigned char)0x02,
	KEY_TYPE_GPIO = (unsigned char)0x03,
} keyType_t;

typedef struct keyAttrAdc_s {
	UINT8 keyNum;
	UINT8 keyType;
	UINT8* pKeyEn;
} keyAttrAdc_t;

typedef struct keyAttrGpio_s {
	UINT8 keyStsPrev;
	UINT8* pKeyEn;
} keyAttrGpio_t;

typedef struct keyMsg_s {
	UINT8 keyMsgPrs;
	UINT8 keyMsgRpt;
	UINT8 keyMsgRls;	
} keyMsg_t;//

typedef struct keyCtrlAdc_s {
	UINT8 keyDec;
	UINT16 code* pKeyVal;
	UINT16 code* pKeyDev;
	UINT16 keyValPrev;
	UINT16 keyValRpt;
} keyCtrlAdc_t;

typedef struct keyCtrlGpio_s {
	UINT8 keyDec;
} keyCtrlGpio_t;

typedef struct keyUnitAdc_s {
	struct keyAttrAdc_s keyAttr;
	struct keyMsg_s code* pKeyMsg;
	struct keyCtrlAdc_s keyCtrl;
	UINT8 keyActiveIdx;
} keyUnitAdc_t;

typedef struct keyUnitGpio_s {
	struct keyAttrGpio_s keyAttr[SP1K_GPIO_BTN_NUM];
	struct keyMsg_s code* pKeyMsg;
	struct keyCtrlGpio_s keyCtrl;
} keyUnitGpio_t;

typedef struct battUnitAdc_s {
	UINT8 battLvl;
	UINT8 battDec;
	UINT8 battDetectEn;
	UINT8 battLvlTol;
	UINT16* pbattAdcVal;
	UINT16 battAdcDev;
} battUnitAdc_t;

typedef struct scanTag_s {
	struct keyUnitAdc_s keyAdcA;
	struct keyUnitAdc_s keyAdcB;
	struct keyUnitGpio_s keyGpio;
	struct battUnitAdc_s battAdc;
} scanTag_t;

#define sp1kBtnDisableAll()	do {\
								sp1kBtnCfg(KEY_TYPE_ADC_A, 0xffffU, 0x00);\
								sp1kBtnCfg(KEY_TYPE_ADC_B, 0xfffeU, 0x00);\
								sp1kBtnCfg(KEY_TYPE_GPIO, 0xffffU, 0x00);\
							} while(0)

#define sp1kBtnEnableAll()	do {\
								sp1kBtnCfg(KEY_TYPE_ADC_A, 0xffffU, 0xff);\
								sp1kBtnCfg(KEY_TYPE_ADC_B, 0xffffU, 0xff);\
								sp1kBtnCfg(KEY_TYPE_GPIO, 0xffffU, 0xff);\
							} while(0)

//------------------------------------------------------------------------------
//Function Prototype
//------------------------------------------------------------------------------
void keyScanInit(void);
void keyScan(void);
UINT8 sp1kBtnCfg(keyType_t BtnType, UINT16 BitMask, UINT8 Enable);
void sp1kAdcHdlGet(const scanTag_t** pAdcHdl);
UINT8 battDetectInit(void);
UINT8 battDetectCfg(UINT8 battLvlTol, UINT16* pBattAdcVal, UINT16 battAdvValDev);
void sp1kBattEnable(UINT8 enable);
UINT8 sp1kBtnChk(UINT8 button);
#endif  /* __KEY_SCAN_H__ */



